package org.rescue.extraction.GraphML;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

public class GraphMLFileHandler  extends DefaultHandler {
	
	    private GMLNodeData gmlNodeData;

	    private Map getAttributeMap(Attributes attrs) {
	        Map map = new HashMap();
	        if (attrs != null) {
	            for (int i = 0; i < attrs.getLength(); i++) {
	                map.put(attrs.getQName(i).toLowerCase(), attrs.getValue(i));
	            }
	        }
	        return map;
	    }


	public void startElement(
	            String namespaceURI,
	            String lName,
	            // local name
	            String qName, // qualified name
	            Attributes attrs) throws SAXException {

	        Map attributeMap = getAttributeMap(attrs);

	        if (qName.toLowerCase().equals("graph")) {
	        	creatGraphData(attributeMap);

	        } else if (qName.toLowerCase().equals("node")) {
	        	gmlNodeData = new GMLNodeData();

	        } else if (qName.toLowerCase().equals("edge")) {
	            createLinkData (attributeMap);

	        }
	    }

	public void endElement(String uri, String localName,
			String qName) throws SAXException {

			if(qName.equalsIgnoreCase("graph")) {

			}else if (qName.equalsIgnoreCase("node")) {
				setNodeData();
			}else if (qName.equalsIgnoreCase("Id")) {
				tempEmp.setId(Integer.parseInt(tempVal));
			}else if (qName.equalsIgnoreCase("name")) {
				tempEmp.setAge(Integer.parseInt(tempVal));
			}

		}
	
	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal = new String(ch,start,length);
	}

    public GMLNodeData getNextNodeData() {
    	return gmlNodeData;
    }

	
	}
